package openase.audiosearch.client.registration.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executors;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import openase.audiosearch.client.registration.controller.AudioRegistrationController;


public class AudioRegistrationFrame extends JFrame {

	private static final long serialVersionUID = -3678964999128615909L;

	private static final String REGISTER = "Register";
	private static final String READING_AUDIO_FILE = "Reading audio file...";
	private static final String REGISTERING_AUDIO = "Registering audio...";

	private AudioRegistrationController controller;

	private JTextField txtFilename = new JTextField(40);
	private JButton btnBrowseFile = new JButton("Browse...");
	private JButton btnRegister = new JButton(REGISTER);

	public AudioRegistrationFrame() {
		super("Open-ASE | Registration client");
		setContentPane(new JPanel(new BorderLayout(5, 5)));
		controller = new AudioRegistrationController();

		buildComponents();

		pack();
		setResizable(false);
	}

	private void buildComponents() {
		JPanel panelFilename = new JPanel();
		panelFilename.add(txtFilename);
		panelFilename.add(btnBrowseFile);
		panelFilename.setBorder(new TitledBorder("Audio file to be registered"));

		JPanel panelRegister = new JPanel();
		panelRegister.add(btnRegister, BorderLayout.CENTER);

		this.add(panelFilename, BorderLayout.NORTH);
		this.add(panelRegister, BorderLayout.SOUTH);

		btnBrowseFile.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				int ret = chooser.showOpenDialog(AudioRegistrationFrame.this);
				if (ret == JFileChooser.APPROVE_OPTION) {
					File selectedFile = chooser.getSelectedFile();
					if (selectedFile != null) {
						try {
							txtFilename.setText(selectedFile.getCanonicalPath());
						} catch (IOException e1) {
							txtFilename.setText(selectedFile.getPath());
						}
					}
				}
			}
		});

		btnRegister.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				final String filename = txtFilename.getText();

				btnRegister.setEnabled(false);

				final Runnable process = new Runnable() {

					public void run() {
						try {
							btnRegister.setText(READING_AUDIO_FILE);
							AudioRegistrationFrame.this.repaint();
							byte[] audio = controller.getAudio(filename);

							String name = new File(filename).getName();
							name = name.substring(0, name.lastIndexOf('.'));

							btnRegister.setText(REGISTERING_AUDIO);
							controller.register(name, audio);

							JOptionPane.showMessageDialog(AudioRegistrationFrame.this, "Audio '" + filename + "' registered!");
						} catch (Throwable t) {
							JOptionPane.showMessageDialog(AudioRegistrationFrame.this, t.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
						} finally {
							btnRegister.setText(REGISTER);
							btnRegister.setEnabled(true);
						}
					}
				};

				Executors.defaultThreadFactory().newThread(process).start();
			}
		});
	}
}
